package enron;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class ComboBoxTableRenderer extends JComboBox implements TableCellRenderer {
	private static final long serialVersionUID = -6365774607949525642L;
	protected TableCellRenderer delegate;

	public ComboBoxTableRenderer(ArrayList<?> data, TableCellRenderer defaultRenderer) {
		delegate = defaultRenderer;
		//identifies TableCellRenderer
		setName("Table.comboBoxCellRenderer");
		setModel(new DefaultComboBoxModel(data.toArray()));
		setBorder(new EmptyBorder(0,0,0,0));
		//((JComponent) this.getEditor().getEditorComponent()).setBorder(new EmptyBorder(0,0,0,0));
		//((JComponent) this.getRenderer().getListCellRendererComponent(new JList(this.getModel()), this.getSelectedItem(), this.getSelectedIndex(), true, this.hasFocus())).setBorder(new EmptyBorder(0,0,0,0));
		setPrototypeDisplayValue("BlahBlahBlahBlahBlah"); //.setPreferredSize(preferredSize)
		setMinimumSize(getPreferredSize());
		//setLayout(new BorderLayout());
		
		//remove component border
		//setBorder(new EmptyBorder(0,0,0,0));
		//disable component background painting
		setOpaque(false);
		//getEditor() .getTextField().setBackground(new Color(0,0,0,0));
		//add(this);
	}

	public Component getTableCellRendererComponent(JTable table, Object value, 
			boolean isSelected, boolean hasFocus, int row, int column) {
		JComponent c = (JComponent)delegate.getTableCellRendererComponent(table, value, 
				isSelected,hasFocus, row, column);
		//respect insets and focus
		//setBorder(c.getBorder());
		//copy opacity
		setOpaque(c.isOpaque());
		//apply background
		setBackground(c.getBackground());    
		//apply foreground
		getEditor(); //.setForeground(c.getForeground());
		//set value
		if(value == null) {
			setSelectedIndex(0);
		} else {
			setSelectedItem(value);
		}
		return this;
	}
}
